package com.zbw.weightlibrary.WeightSub;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.zbw.weightlibrary.Connector.IBleControl;
import com.zbw.weightlibrary.Connector.IinfoCallback;

import cn.wintec.aidl.ScaleInstructionListener;
import cn.wintec.aidl.ScaleListener;
import cn.wintec.aidl.ScaleService;
import cn.wintec.aidl.WintecManagerService;

/**
 * 作者 ：Wesley
 * 时间 ：2022-12-12 16:27
 * WINTECSerialPort 说明：
 * 中科英泰
 */
public class WINTECSerialPort implements IBleControl {
    private String devicePath = "/dev/ttySAC4";
    private WintecManagerService winTec;
    private static ScaleService scaleService;
    private Context mContext;
    private IinfoCallback mCallback;
    private boolean stat = true;
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 获取远程Service的onBind方法返回的对象的代理
            winTec = WintecManagerService.Stub.asInterface(service);
            try {
                Log.e("13","连接成功");
                scaleService = ScaleService.Stub.asInterface(winTec.getScaleService());
                getWeight1();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            winTec = null;
        }
    };

    public WINTECSerialPort(Context context, IinfoCallback callback) {
        this.mContext = context;
        mCallback = callback;
    }

    @Override
    public void bleConnect() {
        Intent intent = new Intent();
        intent.setAction("cn.wintec.SERVICE");
        intent.setPackage("cn.wintec.sdk");
        mContext.bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }

    @Override
    public void bleDisconnect() {
        try {
            if (scaleService != null) {
                scaleService.SCL_Close();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }finally {
            mCallback.onDisconnect("断开设备",true);
        }
    }

    public void getWeight1() {
        try {
            scaleService.SCL_Close();
            scaleService.SCL_Open(devicePath, new ScaleListener.Stub() {
                @Override
                public void onWeightResult(double net, double tare, boolean stable, int i, boolean b1, boolean b2) throws RemoteException {
                  if(mCallback != null && stat){
                      mCallback.onGetWeight(net+"",stable);
                  }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void getWeight() {
        stat = true;
    }

    @Override
    public void stopWeight() {
        stat = false;
    }

    @Override
    public void clearTare() {
        zero();
    }

    @Override
    public void tare() {
        try {
            scaleService.SCL_send_tare(new ScaleInstructionListener.Stub() {
                @Override
                public void onResult(int i) {}
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void zero() {
        try {
            scaleService.SCL_send_zero(new ScaleInstructionListener.Stub() {
                @Override
                public void onResult(int i) {}
            });
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
